package xml1201;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.stream.FileImageInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

//DOM解析
public class TestD4 {
	static Document document = null;
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
		
		System.out.println(3);
	}

	 void updateInfo() throws ParserConfigurationException, SAXException,
			IOException, TransformerException{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		InputStream in = new FileInputStream("收藏信息.xml");
		document = builder.parse(in);
		NodeList list = document.getElementsByTagName("brand");
		for (int i = 0; i < list.getLength(); i++) {
			Node node = list.item(i);
			Element element = (Element) node;
			String s = element.getAttribute("name");
			System.out.println(s);
			NodeList list1 = element.getChildNodes();
			for (int j = 0; j < list1.getLength(); j++) {
				Node node1 = list1.item(j);
				if (node1.getNodeType() == node1.ELEMENT_NODE) {
					Element element2 = (Element) node1;
					System.out.println(element2.getAttribute("name"));
					save("收藏信息.xml");
				}
			}
		}
	}

	 void save(String path) throws TransformerException {
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		Source xmlSource = new DOMSource(document);
		transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		Result outputTarget = new StreamResult(new File("D:\\lllll.xml"));
		transformer.transform(xmlSource, outputTarget);

	}

	// 添加手机收藏
	public void add() throws ParserConfigurationException, SAXException,
			IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		document = builder.parse("收藏信息.xml");
		Element e1 = document.createElement("brand");
		e1.setAttribute("name", "三星");
		Element e2 = document.createElement("Type");
		e2.setAttribute("name", "大哥大");
		e1.appendChild(e2);
		System.out.println(1);
		document.getElementsByTagName("phoneInfo").item(0).appendChild(e1);
		System.out.println(2);
	 
	 }

	// 修改删除手机收藏
	// 1、得到DOM解析器的工厂实例
	 void delete() throws ParserConfigurationException, SAXException,
			IOException, TransformerException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		 // 2、从DOM工厂获得DOM解析器
		DocumentBuilder db = dbf.newDocumentBuilder();
		// 3、解析XML文档，得到一个Document，即DOM树
		document = db.parse("收藏信息.xml");
		NodeList list = document.getElementsByTagName("brand");
		for (int i = 0; i < list.getLength(); i++) {
			Element element = (Element) list.item(i);
			if (element.getAttribute("name").equals("华为")) {
				element.getParentNode().removeChild(element);
			}
		}
		save("收藏信息.xml");
	}

	 void show() throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		document = db.parse("src/收藏信息.xml");
		NodeList list = document.getElementsByTagName("brand");
		for (int i = 0; i < list.getLength(); i++) {
			Element element = (Element)list.item(i);
			if(element.getNodeType()!=-1){
				continue;
			}//输出标签的name属性
			Element element2 =(Element)element;
			System.out.println("品牌"+element2.getAttribute("name"));
			//获取Brand标签的所有子节点
			 NodeList list2=element.getChildNodes();
			 for (int j = 0; j < list2.getLength(); j++) {
				 Element element3 = (Element)list2;
				 if(element3.getNodeType()!=1){
					 continue;
				 }
				 element2 =(Element)element3;
				 System.out.println("型号\t"+element2.getAttribute("name"));
				
			}
		}
	}

}
